package com.open.capacity.finance.controller;

import java.util.Map;

import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.Result;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.utils.SysUserUtil;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.finance.entity.SysPrintTemplete;
import com.open.capacity.finance.service.SysPrintTempleteService;

import com.open.capacity.commons.PageResult;

import javax.validation.Valid;

/**
 * 打印模板
 *
 * @author
 * @email
 * @date 2020-06-17 10:27:52
 */
@RestController
@RequestMapping("printTempletes")
@Api(tags = "打印模板")
public class SysPrintTempleteController {

    @Autowired
    private SysPrintTempleteService sysPrintTempleteService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('sys_print_templete:sysprinttemplete:list')")
    public PageResult list(@RequestParam Map<String, Object> params) {
        PageResult pageResult = sysPrintTempleteService.pageAll(params);
        return pageResult;
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('sys_print_templete:sysprinttemplete:list')")
    public Result findById(String id) {
        return Result.succeed(sysPrintTempleteService.findById(id));
    }

    /**
     * 查询单条记录
     */
    @RequestMapping("/findByCode")
    @PreAuthorize("hasAnyAuthority('sys_print_templete:sysprinttemplete:list')")
    public Result findByCode(String companyId, String ztId, String code) {
        return Result.succeed(sysPrintTempleteService.findByCode(companyId, ztId, code));
    }


    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('sys_print_templete:sysprinttemplete:save')")
    @LogAnnotation(module = "打印模板", name = "更新")
    public Result save(@RequestBody SysPrintTemplete templete, String keyValue) {
        SysUser loginUser = SysUserUtil.getLoginAppUser();
        templete.setUpdateUsername(loginUser.getRealName());
        sysPrintTempleteService.update(templete);
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('sys_print_templete:sysprinttemplete:delete')")
    @LogAnnotation(module = "打印模板", name = "删除")
    public Result delete(@PathVariable String id) {
        sysPrintTempleteService.delete(id);
        return Result.succeed("删除成功");
    }

}
